﻿using EMall.Query;
using IdentityServer4.Models;
using IdentityServer4.Stores;
using Microsoft.EntityFrameworkCore;

namespace EMall.AuthServer
{
    public class IdentityServerClientStore : IClientStore
    {
        private readonly IQueryService _queryService;
        public IdentityServerClientStore(IQueryService queryService)
        {
            _queryService = queryService;
        }
        public async Task<Client> FindClientByIdAsync(string clientId)
        {
            var entity = await _queryService.GetQueryable<IdentityServer4.EntityFramework.Entities.Client>()
                .Include(x => x.AllowedCorsOrigins)
                .Include(x => x.AllowedGrantTypes)
                .Include(x => x.AllowedScopes)
                .Include(x => x.Claims)
                .Include(x => x.ClientSecrets)
                .Include(x => x.IdentityProviderRestrictions)
                .Include(x => x.PostLogoutRedirectUris)
                .Include(x => x.Properties)
                .Include(x => x.RedirectUris)
                .FirstOrDefaultAsync(o => o.ClientId == clientId);
            var model = entity!.ToModel();
            return model;
        }
    }
}
